#include "../header/common.h"
#define ANZAHLMENU 6


void zeige_maus (void)
{
 SDL_Cursor *cursor;
   cursor = SDL_CreateCursor (NULL, NULL, 8, 8, 8, 8);
  SDL_SetCursor (cursor);
}
int maus_klick(int x,int y,SDL_Rect dist[ANZAHLMENU],SDL_Surface *screen,SDL_Surface *image[],int s)
{
  SDL_Surface *background;
  int i,restart=0,exit,click;
  if(screen->w==640)background = SDL_LoadBMP("image/640480/menubackground.bmp");
  if(screen->w==800)background = SDL_LoadBMP("image/800600/menubackground.bmp");
  if(screen->w==1024)background = SDL_LoadBMP("image/1024768/menubackground.bmp");
  for (i=0;i<ANZAHLMENU;i++)
    {
      click=click_button(dist[i],x,y);
      if (click)
	{
	  switch(i)
	    {
	    case 0:
	      exit=name(screen,1); 
	      if(s){play(0);play(2);} 
	      if(exit==1)return 1;
	      break;
	    case 1:
	      exit=singleplayer(screen,2,NULL);
	      if(s){play(0);play(2);} 
	      if(exit==1)return 1;
	      break;
	    case 2:
	      exit=name(screen,3);
	      if(s){play(0);play(2);} 
	      if(exit==1)return 1;
	      break;
	    case 3: 
	      restart=options_menu(screen); 
	      if(restart==2)return 2;
	      if(restart==1)return 1;
	      break;
	    case 4: 
	      exit=(highscore(0,screen,NULL,0));
 	      if(exit==1)return 1; 
	      break;
	    case 5:
	      return 1;
	    default:  break;				
	    }
	  SDL_BlitSurface(background,NULL,screen,NULL);
	  for(i=0;i<ANZAHLMENU;i++)
	    {
	      SDL_BlitSurface(image[i], NULL, screen, &dist[i]);   	
	    }
	  SDL_Flip(screen);
	}      
    }
  return(0);
}

int menu(SDL_Surface *screen)
{
  SDL_Surface *image[ANZAHLMENU],*imagepressed[ANZAHLMENU],*background;
  SDL_Rect dst[ANZAHLMENU];
  SDL_Event event;
  char bmp_file[]="image/640480/buttons/menu#.bmp";
  char bmp_file_pressed[]="image/640480/buttonspressed/amenu#.bmp";
  int done = 0, i, x, y,click=0;
  int r,s;
  Uint8 SDL_GetRelativeMouseState(int *x, int *y);
  SDL_MouseMotionEvent mm;
  SDL_MouseButtonEvent mb;
  options(1,&s,&r);
  if(screen->w==640)
    {
      background = SDL_LoadBMP("image/640480/menubackground.bmp");
    } 
  if(screen->w==800)
    {
      background = SDL_LoadBMP("image/800600/menubackground.bmp"); 
    }
  if(screen->w==1024)
    {
      background = SDL_LoadBMP("image/1024768/menubackground.bmp");
    }
  SDL_BlitSurface(background,NULL,screen,NULL);
  if(s)play(2);
  zeige_maus();
  for(i=0;i<ANZAHLMENU;i++)
    {
      bmp_file[25] = i+48;
      image[i] = SDL_LoadBMP (bmp_file);
      bmp_file_pressed[33] = i+48;
      imagepressed[i] = SDL_LoadBMP (bmp_file_pressed);
      dst[i].x = ((screen->w/2)-(image[i]->w/2));
      dst[i].y = ((screen->h/4)+ image[i]->h*i);
      dst[i].w = image[i]->w;
      dst[i].h = image[i]->h;
      SDL_SetColorKey(image[i], SDL_SRCCOLORKEY, SDL_MapRGB(image[i]->format, 255, 255, 255)); 
      SDL_SetColorKey(imagepressed[i], SDL_SRCCOLORKEY, SDL_MapRGB(imagepressed[i]->format, 255, 255, 255)); 
      SDL_BlitSurface(image[i], NULL, screen, &dst[i]);   	
    }
  SDL_Flip(screen);  
  while (!done) 
    {
      while (SDL_PollEvent(&event)) 
	{
	  switch (event.type) 
	    {
	    case SDL_QUIT:
	      done=1;
	      break;
	    
	    case SDL_KEYDOWN:
	      switch(event.key.keysym.sym)
		{
		case SDLK_ESCAPE:
		  done=1;
		  break;
		}
	    case SDL_MOUSEBUTTONDOWN:
	      mb=event.button;
	      done=maus_klick(mb.x,mb.y,dst,screen,image,s);	 
	      /* Mausklick abfangen */
	      
	      if(done==2)return 2;
	      if(done==1)return 1;
	      break;
	      
	    case SDL_MOUSEMOTION:
	      mm=event.motion;	  
	      for(i=0;i<ANZAHLMENU;i++)
		{
		  click=click_button(dst[i],mm.x,mm.y);
		  switch(click)
		    {
		    case 0:
		      SDL_BlitSurface(image[i],NULL,screen,&dst[i]);
		      break;
		    case 1:
		      SDL_BlitSurface(imagepressed[i],NULL,screen,&dst[i]);
		      break;
		      
		    }
		}
	    }
	}
      SDL_Flip(screen);
    }
  for(i=0;i<ANZAHLMENU;i++)
    {
      SDL_FreeSurface(image[i]);
      SDL_FreeSurface(imagepressed[i]);
    }
  return 0;
}
